﻿using Foundation.Core;
using Foundation.Uow;
using Microsoft.Extensions.DependencyInjection;

namespace Dyao.Book.Web.Extensions
{
    public class ServiceMethodSelectCollectionUniOfWrokCreator : ServiceMethodSelectCollectionCreator
    {
        private readonly IServiceProvider _serviceProvider;

        public ServiceMethodSelectCollectionUniOfWrokCreator(SelectItemLoadMethodOptions methodOptions, IServiceProvider serviceProvider)
            : base(methodOptions, serviceProvider)
        {
            this._serviceProvider = serviceProvider;
        }

        public async override Task<SelectCollection> CreateAsync(string key, SelectCategory category, IDictionary<string, object> arguments, CancellationToken cancellationToken)
        {
            var manager = _serviceProvider.GetService<IUnitOfWorkManager>();

            using (manager.Begin())
            {
                return await base.CreateAsync(key, category, arguments, cancellationToken);
            }
        }
    }
}
